
package com.eworking.eim.encrypt;

import java.io.File;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;

import android.util.Base64;

import com.eworking.eim.EIMChatManager;
import com.eworking.eim.util.EIMLog;
import com.eworking.eim.util.EasyUtil;

public class EIMEncryptUtils {
    
    /** 日志输出标记 */
    private static final String _TAG = EIMEncryptUtils.class.getSimpleName();
    
    public static String encryptMessage(String paramString1, String paramString2) {
        try {
            EIMEncryptProvider encryptProvider = EIMChatManager.getInstance().getEncryptProvider();
            byte[] arrayOfByte1 = paramString1.getBytes("UTF-8");
            EIMLog.d(_TAG, "utf-8 bytes:" + EasyUtil.convertByteArrayToString(arrayOfByte1));
            byte[] arrayOfByte2 = encryptProvider.encrypt(arrayOfByte1, paramString2);
            EIMLog.d(_TAG, "encrypted bytes:" + EasyUtil.convertByteArrayToString(arrayOfByte2));
            byte[] arrayOfByte3 = Base64.encode(arrayOfByte2, 0);
            EIMLog.d(_TAG, "base64 bytes:" + EasyUtil.convertByteArrayToString(arrayOfByte3));
            String str = new String(arrayOfByte3);
            EIMLog.d(_TAG, "encrypted str:" + str);
            return str;
        } catch (Exception localException) {
            localException.printStackTrace();
            EIMLog.e(_TAG, "encryption error, send plain msg");
            return paramString1;
        }
    }
    
    public static String decryptMessage(String paramString1, String paramString2) {
        try {
            EIMLog.d(_TAG, "encrypted str:" + paramString1);
            byte[] arrayOfByte1 = Base64.decode(paramString1, 0);
            EIMLog.d(_TAG, "base64 decode bytes:" + EasyUtil.convertByteArrayToString(arrayOfByte1));
            EIMEncryptProvider encryptProvider = EIMChatManager.getInstance().getEncryptProvider();
            byte[] arrayOfByte2 = encryptProvider.decrypt(arrayOfByte1, paramString2);
            EIMLog.d(_TAG, "decrypt bytes:" + EasyUtil.convertByteArrayToString(arrayOfByte2));
            String str = new String(arrayOfByte2, "UTF-8");
            EIMLog.d(_TAG, "descripted str:" + str);
            return str;
        } catch (Exception localException) {
            EIMLog.e(_TAG, "decrypted file:" + paramString1);
            return paramString1;
        }
    }
    
    @SuppressWarnings("resource")
    public static String encryptFile(String paramString1, String paramString2) {
        try {
            EIMLog.d(_TAG, "try to encrypt file:" + paramString1);
            RandomAccessFile localRandomAccessFile = new RandomAccessFile(paramString1, "r");
            int i = (int) localRandomAccessFile.length();
            EIMLog.d(_TAG, "try to encrypt file:" + paramString1 + " original len:" + i);
            byte[] arrayOfByte1 = new byte[i];
            int j = localRandomAccessFile.read(arrayOfByte1);
            if (j != i) {
                EIMLog.e(_TAG, "error read file, file len:" + i + " readLen:" + j);
                return paramString1;
            }
            localRandomAccessFile.close();
            EIMEncryptProvider encryptProvider = EIMChatManager.getInstance().getEncryptProvider();
            byte[] arrayOfByte2 = encryptProvider.encrypt(arrayOfByte1, paramString2);
            String str1 = null;
            int k = paramString1.lastIndexOf(46);
            if (k >= 0)
                str1 = paramString1.substring(k);
            File localFile = File.createTempFile("encrypted", str1);
            FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
            localFileOutputStream.write(arrayOfByte2);
            localFileOutputStream.close();
            String str2 = localFile.getAbsolutePath();
            EIMLog.d(_TAG, "generated encrypted file:" + str2);
            return str2;
        } catch (Exception localException) {
            EIMLog.e(_TAG, "decrypted file:" + paramString1);
            return paramString1;
        }
    }
    
    public static void decryptFile(String paramString1, String paramString2) {
        try {
            EIMLog.d(_TAG, "decrypt file:" + paramString1);
            RandomAccessFile localRandomAccessFile = new RandomAccessFile(paramString1, "r");
            int i = (int) localRandomAccessFile.length();
            byte[] arrayOfByte1 = new byte[i];
            int j = localRandomAccessFile.read(arrayOfByte1);
            if (j != i) {
                EIMLog.e(_TAG, "error read file, file len:" + i + " readLen:" + j);
            } else {
                localRandomAccessFile.close();
                EIMEncryptProvider encryptProvider = EIMChatManager.getInstance().getEncryptProvider();
                byte[] arrayOfByte2 = encryptProvider.decrypt(arrayOfByte1, paramString2);
                FileOutputStream localFileOutputStream = new FileOutputStream(paramString1, false);
                localFileOutputStream.write(arrayOfByte2);
                localFileOutputStream.close();
            }
            EIMLog.d(_TAG, "decrypted file:" + paramString1);
        } catch (Exception localException) {
            EIMLog.e(_TAG, "decrypted file:" + paramString1);
        }
    }
}
